package cn.fotoit.product.app1.controller;
import cn.fotoit.product.app1.entity.Menu;
import cn.fotoit.product.app1.service.MenuService;
import cn.fotoit.product.util.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("add")
    public ReturnBean add(
            @RequestParam String name,
            String url,
            @RequestParam Integer orders,
            @RequestParam Boolean isOnline,
            @RequestParam Boolean isHot,
            @RequestParam Boolean isMustLogin,
            Boolean deleted,
            byte[] imgByte,
            @RequestParam Integer groupId) {
        Menu menu = new Menu();
        menu.setName(name);
        menu.setUrl(url);
        menu.setOrders(orders);
        menu.setIsOnline(isOnline);
        menu.setIsHot(isHot);
        menu.setIsMustLogin(isMustLogin);
        menu.setDeleted(deleted);
        menu.setImgByte(imgByte);
        menu.setGroupId(groupId);
        menuService.add(menu);
        return ReturnBean.success("添加成功");
    }

    @PostMapping("updateNotNull")
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            String name,
            String url,
            Integer orders,
            Boolean isOnline,
            Boolean isHot,
            Boolean isMustLogin,
            Boolean deleted,
            byte[] imgByte,
            Integer groupId) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setName(name);
        menu.setUrl(url);
        menu.setOrders(orders);
        menu.setIsOnline(isOnline);
        menu.setIsHot(isHot);
        menu.setIsMustLogin(isMustLogin);
        menu.setDeleted(deleted);
        menu.setImgByte(imgByte);
        menu.setGroupId(groupId);
        menuService.updateNotNull(menu);
        return ReturnBean.success("修改成功");
    }

    @PostMapping("deleteById")
    public ReturnBean deleteById(@RequestParam Integer id) {
        menuService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", menuService.findAll(page, size));
    }

    @PostMapping("findById")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", menuService.findById(id));
    }


}
